﻿using Apps.BLL.Core;
using Apps.Common;
using Apps.Locale;
using Apps.Models;
using Apps.Models.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.BLL.API
{
    public partial class API_RELATIONBLL
    {
        public override bool Create(ref ValidationErrors errors, API_RELATIONModel model)
        {
            try
            {
                var find = this.GetPagerList(ref GridPager.FirstRow,p=> p.ROLE_ID==model.ROLE_ID && p.API_ID==model.API_ID);
                if (find.Count>0)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                API_RELATION entity = new API_RELATION();
                entity.ROLE_ID = model.ROLE_ID;
                entity.API_ID = model.API_ID;


                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public bool DeleteByModel(ref ValidationErrors errors, API_RELATIONModel model)
        {
            try
            {
                if (m_Rep.Delete(p=> p.API_ID == model.API_ID && p.ROLE_ID==model.ROLE_ID)>0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }
    }
}
